package com.office.personalOffice.personalInfo.service.impl;

import com.office.personalOffice.personalInfo.dao.IPersonalInfo;
import com.office.personalOffice.personalInfo.model.ResponseResult;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.personalOffice.personalInfo.service.IPersonalInfoService;
import com.office.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PersonalInfoService implements IPersonalInfoService {

    @Autowired
    private IPersonalInfo personalInfo;
//    @Autowired
//    private PasswordEncoder passwordEncoder;

    public IPersonalInfo getPersonalInfo() {
        return personalInfo;
    }

    public void setPersonalInfo(IPersonalInfo personalInfo) {
        this.personalInfo = personalInfo;
    }

    /**
     * 根据个条件查询某一个员工信息
     * @param tbStaff
     * @return
     */
    @Override
    public TbStaff getStaffByCondition(TbStaff tbStaff) {

        TbStaff staffByCondition = personalInfo.getStaffByCondition(tbStaff);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String flag = staffByCondition.getFlag();
        String[] split = flag.split(";");
        ArrayList<Integer> integers = new ArrayList<>();
        for (String s : split) {
            Integer valueOf = Integer.valueOf(s);
            integers.add(valueOf);
        }
        staffByCondition.setFlagList(integers);
        return  staffByCondition;
    }

    /**
     * 获取所有员工信息
     * @return
     */
    @Override
    public List<TbStaff> getStaffList() {
        return personalInfo.getStaffList();
    }

    /**
     * 更新员工信息
     * @param tbStaff
     * @return
     */
    @Override
    public Integer updateStaffInfo(TbStaff tbStaff) {
        String flag = tbStaff.getFlag();
        String usinfFlag="";
        char[] strings = flag.toCharArray();
        for(int i=0;i<strings.length;i++){
            if(i==(strings.length-1)){
                usinfFlag+=strings[i];
            }else {
                usinfFlag+=strings[i]+";";
            }
        }
        tbStaff.setFlag(usinfFlag);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(tbStaff.getPassword());
        tbStaff.setPassword(encode);
        return personalInfo.updateStaffInfo(tbStaff);
    }

    /**
     * 更新员工密码
     * @param tbStaff
     * @return
     */
    @Override
    public Integer updateStaffPassword(TbStaff tbStaff) {
        String password = tbStaff.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        tbStaff.setPassword(encode);
        return personalInfo.updateStaffPassword(tbStaff);
    }

    //查询邮箱是否已存在
    @Override
    public TbStaff checkEmail(TbStaff tbStaff) {
        TbStaff tbStaff1 = new TbStaff();
        tbStaff1.setEmail(tbStaff.getEmail());
        TbStaff staffByCondition = personalInfo.getStaffByCondition(tbStaff1);
        if(staffByCondition!=null){
            if(staffByCondition.getId()==tbStaff.getId()){
                return null;
            }
        }

        return staffByCondition;
    }

    //查询电话号码是否已存在
    @Override
    public TbStaff checkTel(TbStaff tbStaff){
        TbStaff tbStaff1 = new TbStaff();
        tbStaff1.setTel(tbStaff.getTel());
        TbStaff staffByCondition = personalInfo.getStaffByCondition(tbStaff1);
        if(staffByCondition!=null){
            if(staffByCondition.getId()==tbStaff.getId()){
                return null;
            }
        }
        return staffByCondition;
    }

    //查询员工号是否已存在
    @Override
    public TbStaff checkStaffId(TbStaff tbStaff) {
        TbStaff tbStaff1 = new TbStaff();
        tbStaff1.setStaffId(tbStaff.getStaffId());
        TbStaff staffByCondition = personalInfo.getStaffByCondition(tbStaff1);
        if(staffByCondition!=null){
            if(staffByCondition.getId()==tbStaff.getId()){
                return null;
            }
        }
        return staffByCondition;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public ResponseResult login(String username, String password) {
        TbStaff staff = personalInfo.login(username);
        if (staff == null) {
            return new ResponseResult(400,"用户不存在",null);
        } else {
            String subject;
            try {
                Claims claims = JwtUtil.parseJWT(staff.getPassword());
                subject = claims.getSubject();
            } catch (Exception e) {
                throw new RuntimeException("密码有误");
            }
            if (subject.equals(password)) {
                return new ResponseResult(401,"密码成功",staff);
            } else {
                return new ResponseResult(401,"密码错误",null);
            }
        }
    }

    /**
     * 查看用户是否存在
     * @param name
     * @return
     */
    @Override
    public String userExist(String name) {
        TbStaff staff = personalInfo.login(name);
        if (staff==null){
            return "不存在此用户";
        }
        return "ok";
    }

}
